#!/usr/bin/env bash
index_no="01"
version="8.0"
containerName="mysql-dev-${index_no}-${version}"
password="123456@#^^^"
port="33061"
install_dir="/docker/mysql/"
install_index_no_dir=$install_dir$index_no
conf=$install_dir$index_no/conf/
data=$install_dir$index_no/data/

echo '>>> Please later ... ...'


if [ ! -d $install_index_no_dir ];then
  mkdir -p $install_index_no_dir
else
  cd $install_index_no_dir
  rm -rf ./*
fi
echo "开始创建并复制映射目录..."
mkdir -p $install_index_no_dir/conf
mkdir -p $data
cp -f ${install_dir}my.cnf $conf

set -e
#sudo docker pull mysql |  grep "Image is up to date" || (echo Already up to date. Exiting... && exit 0)

echo '>>> Get old container id'
CID=$(sudo docker ps --all | grep ${containerName} | awk '{print $1}')
echo $CID
echo ">>> Stopping and deleting old container ${containerName}"
if [ "$CID" != "" ];
then
  sudo docker stop $CID
  sudo docker rm $CID
fi

tempMountCommand="docker run --name ${containerName} -p ${port}:3306 -e MYSQL_ROOT_PASSWORD=${password} -e TZ=Asia/Shanghai -d   --privileged=true  --restart=always  -v ${conf}my.cnf:/etc/mysql/my.cnf -v ${data}:/var/lib/mysql/  mysql:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"
echo ">>> 执行挂载的命令： ${tempMountCommand}"
# 挂载
$tempMountCommand
echo ">>> synchronization time zone"
docker cp /usr/share/zoneinfo/Asia/Shanghai ${containerName}:/usr/share/zoneinfo/Asia 